#include "LightEntity.h"
#include "Common.h"
#include "resource.h"

namespace FinalRendering
{
	namespace Plugin
	{
		RefPtr<GxRendering::UI::Image> LightDesc::logoImage;
		const wchar_t * lightClassName = L"Light";
		LightDesc lightDesc;

		Light::Light()
		{
			DistanceHalf = DistanceHalf = CutOff = 0.0f;
			Power = 1.0f;
		}

		Light::Light(Stream * stream)
			:PointEntity(stream)
		{
			stream->ReadBuffer(&Color,1);
			stream->ReadBuffer(&Dir,1);
			stream->ReadBuffer(&DistanceHalf,1);
			stream->ReadBuffer(&DistanceFull,1);
			stream->ReadBuffer(&CutOff,1);
			stream->ReadBuffer(&Power,1);
		}

		bool Light::Serialize(Stream * stream)
		{
			PointEntity::Serialize(stream);
			stream->WriteBuffer(&Color,1);
			stream->WriteBuffer(&Dir,1);
			stream->WriteBuffer(&DistanceHalf,1);
			stream->WriteBuffer(&DistanceFull,1);
			stream->WriteBuffer(&CutOff,1);
			stream->WriteBuffer(&Power,1);
			return true;
		}
		int Light::ProcessMessage(IEntity * sender, EntityMessage msg)
		{
			if (wcscmp(msg.Message, L"GetColor") == 0)
			{
				*((vec3 *)msg.Param) = Color;
			}
			else if (wcscmp(msg.Message, L"GetDir") == 0)
			{
				*((vec3 *)msg.Param) = Dir;
			}
			else if (wcscmp(msg.Message, L"GetDistanceHalf") == 0)
			{
				*((float *)msg.Param) = DistanceHalf;
			}
			else if (wcscmp(msg.Message, L"GetDistanceFull") == 0)
			{
				*((float *)msg.Param) = DistanceFull;
			}
			else if (wcscmp(msg.Message, L"GetCutOff") == 0)
			{
				*((float *)msg.Param) = CutOff;
			}
			else if (wcscmp(msg.Message, L"GetPower") == 0)
			{
				*((float *)msg.Param) = Power;
			}
			else
				PointEntity::ProcessMessage(sender, msg);
			return 1;
		}
		IEntityDesc * Light::GetDesc()
		{
			return &lightDesc;
		}
		int Light::GetPropertyCount()
		{
			return 7;
		}
		PropertyDesc Light::GetPropertyDesc(int i)
		{
			PropertyDesc rs;
			if (i<PointEntity::GetPropertyCount())
				return PointEntity::GetPropertyDesc(i);
			switch (i)
			{
			case 1:
				rs.PropertyName = L"Color";
				rs.PropertyType = L"Color";
				rs.Value = &Color;
				break;
			case 2:
				rs.PropertyName = L"Dir";
				rs.PropertyType = L"Vec3";
				rs.Value = &Dir;
				break;
			case 3:
				rs.PropertyName = L"DistanceHalf";
				rs.PropertyType = L"Float";
				rs.Value = &DistanceHalf;
				break;
			case 4:
				rs.PropertyName = L"DistanceFull";
				rs.PropertyType = L"Float";
				rs.Value = &DistanceFull;
				break;
			case 5:
				rs.PropertyName = L"CutOff";
				rs.PropertyType = L"Float";
				rs.Value = &CutOff;
				break;
			case 6:
				rs.PropertyName = L"Power";
				rs.PropertyType = L"Float";
				rs.Value = &Power;
				break;
			}
			return rs;
		}
		const wchar_t * LightDesc::ClassName()
		{
			return lightClassName;
		}
		IEntity * LightDesc::Create()
		{
			return new Light();
		}
		void LightDesc::Destroy(IEntity * ent)
		{
			delete ent;
		}
		IEntity * LightDesc::Create(Stream * stream)
		{
			return new Light(stream);
		}
		
		IEntityDesc * LightDesc::GetDesc()
		{
			return &lightDesc;
		}
		void Light::EditorDraw(IView *view)
		{
			DrawEntity(this, view, Position, LightDesc::logoImage);
		}

		void Light::InitPlugin(bool isEditor, IEngine * _engine, HMODULE module)
		{
			if (isEditor)
			{
				LightDesc::logoImage = new GxRendering::UI::Image(&GxWinGUI::Drawing::Bitmap(IDB_ENT_LIGHT,(HMODULE)module), &GxRendering::UI::Color(0,0,0,0));
			}
		}

		void Light::DestroyPlugin()
		{
			LightDesc::logoImage = 0;
		}
	}
}